//
//  CMCTabbarViewController.m
//  
//
//  Created by Oliver Farago on 19/06/2010.
//  Copyright 2010 CMC Software All rights reserved.
//

#import "CMCTabbarViewController.h"
#import "Globals.h"
#import "Utilities.h"
#define TABBAR_ICON_WIDTH   (320/4)
#define TABBAR_ICON_HEIGHT  55
#define NUMBER_OF_BUTTON    4
#define TABBAR_TEXT_HEIGHT  20
#define TABBAR_TEXT_FONT_SIZE 9
#define TABBAR_BUTTON_OFFSET APP_SCREEN_HEIGHT+20 - 50
#define TABBAR_TEXT_WIDTH   (320/4)


@implementation CMCTabbarViewController

@synthesize btn1, btn2, btn3, btn4;
@synthesize backgroundView;
@synthesize active1, active2, active3, active4;

- (id)init{
    [super init];
    if (self) {
        DBLOG(@"tabbar did appear");
        [self hideTabBar];
        [Utilities hideTabBar:nil];
        [self addCustomElements];
    }
    return self;
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	
}

- (void)hideTabBar
{
	for(UIView *view in self.view.subviews)
	{
		if([view isKindOfClass:[UITabBar class]])
		{
			view.hidden = YES;
			break;
		}
	}
}

- (void)hideNewTabBar 
{
    self.btn1.hidden = 1;
    self.btn2.hidden = 1;
    self.btn3.hidden = 1;
    self.btn4.hidden = 1;
    [self.active1 setHidden:YES];
    [self.active2 setHidden:YES];
    [self.active3 setHidden:YES];
    [self.active4 setHidden:YES];
    [self.backgroundView setHidden:YES];
}

- (void)showNewTabBar
{
    [self hideTabBar];
    self.btn1.hidden = 0;
    self.btn2.hidden = 0;
    self.btn3.hidden = 0;
    self.btn4.hidden = 0;
    if (self.selectedIndex == 0) {
        [self.active1 setHidden:NO];
    }
    else if (self.selectedIndex == 1) {
        [self.active2 setHidden:NO];
    }
    else if (self.selectedIndex == 2) {
        [self.active3 setHidden:NO];
    }
    else if (self.selectedIndex == 3) {
        [self.active4 setHidden:NO];
    }
    [self.backgroundView setHidden:NO];

}

-(void)addCustomElements
{
    DBLOG(@"Tab Bar Adds Custome Elements");
    // TODO: add the tab bar background image
    backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabbar_bg.png"]];
    [backgroundView setFrame:CGRectMake(0,
                                        TABBAR_BUTTON_OFFSET+2,
                                        320,
                                        TABBAR_ICON_HEIGHT-5)];
    [backgroundView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:backgroundView];
    
    // TODO: configure view of the first bar button: Reacticons
	// Initialise our two images
	UIImage *btnImage = [UIImage imageNamed:@"bg_reacticon_inactive.png"];
	
	btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
    btn1.frame = CGRectMake(0 + (TABBAR_TEXT_WIDTH - TABBAR_ICON_WIDTH)/2,
                            TABBAR_BUTTON_OFFSET+0.5,
                            TABBAR_ICON_WIDTH,
                            TABBAR_ICON_HEIGHT-0.5);
	[btn1 setImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button
	[btn1 setTag:TumblrTab]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
	[btn1 setSelected:true]; // Set this button as selected (we will select the others to false as we only want Tab 1 to be selected initially
	UIImage *btnImageSelected = [UIImage imageNamed:@"bg_reacticon_active.png"];
    active1 = [[UIImageView alloc] initWithImage:btnImageSelected];
    [active1 setFrame:CGRectMake(-5,
                                 TABBAR_BUTTON_OFFSET - 5,
                                 91,
                                 TABBAR_ICON_HEIGHT)];
    
    // TODO: configure view of the second bar button: Favorites
	btnImage = [UIImage imageNamed:@"bg_favorites_inactive.png"];
	btnImageSelected = [UIImage imageNamed:@"bg_favorites_active.png"];
	btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(1*TABBAR_TEXT_WIDTH + (TABBAR_TEXT_WIDTH - TABBAR_ICON_WIDTH)/2,
                            TABBAR_BUTTON_OFFSET,
                            TABBAR_ICON_WIDTH,
                            TABBAR_ICON_HEIGHT);
	[btn2 setImage:btnImage forState:UIControlStateNormal];
	[btn2 setTag:FavoriteTab];
    active2 = [[UIImageView alloc] initWithImage:btnImageSelected];
    [active2 setFrame:CGRectMake(TABBAR_ICON_WIDTH + (TABBAR_ICON_WIDTH - btnImageSelected.size.width)/2,
                                 TABBAR_BUTTON_OFFSET - 5,
                                 btnImageSelected.size.width,
                                 TABBAR_ICON_HEIGHT)];
    	
    // TODO: configure view of the third bar button: Custom
	btnImage = [UIImage imageNamed:@"bg_custom_inactive.png"];
	btnImageSelected = [UIImage imageNamed:@"bg_custome_active.png"];
	btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame = CGRectMake(2*TABBAR_TEXT_WIDTH + (TABBAR_TEXT_WIDTH - TABBAR_ICON_WIDTH)/2,
                            TABBAR_BUTTON_OFFSET,
                            TABBAR_ICON_WIDTH,
                            TABBAR_ICON_HEIGHT);
	[btn3 setImage:btnImage forState:UIControlStateNormal];
	[btn3 setTag:CustomTab];
    active3 = [[UIImageView alloc] initWithImage:btnImageSelected];
    [active3 setFrame:CGRectMake(2*TABBAR_ICON_WIDTH + (TABBAR_ICON_WIDTH - btnImageSelected.size.width)/2,
                                 TABBAR_BUTTON_OFFSET - 5,
                                 btnImageSelected.size.width,
                                 TABBAR_ICON_HEIGHT)];
    
	
    // TODO: configure the forth bar button: More
	btnImage = [UIImage imageNamed:@"bg_more_inactive.png"];
	btnImageSelected = [UIImage imageNamed:@"bg_more_active.png"];
	btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn4.frame = CGRectMake(-1+3*TABBAR_TEXT_WIDTH + (TABBAR_TEXT_WIDTH - TABBAR_ICON_WIDTH)/2,
                            TABBAR_BUTTON_OFFSET+3,
                            TABBAR_ICON_WIDTH,
                            TABBAR_ICON_HEIGHT-3.5);
    [btn4 setBackgroundImage:btnImage forState:UIControlStateNormal];
//	[btn4 setImage:btnImage forState:UIControlStateNormal];
    [btn4 setHidden:NO];
	[btn4 setTag:MoreTab];
    active4 = [[UIImageView alloc] initWithImage:btnImageSelected];
    [active4 setFrame:CGRectMake(3*TABBAR_ICON_WIDTH + (TABBAR_ICON_WIDTH - btnImageSelected.size.width)/2,
                                 TABBAR_BUTTON_OFFSET - 5,
                                 btnImageSelected.size.width,
                                 TABBAR_ICON_HEIGHT)];
    
	// Add my new buttons to the view
	[self.view addSubview:btn1];
	[self.view addSubview:btn2];
	[self.view addSubview:btn3];
    [self.view addSubview:btn4];
    [self.view addSubview:active1];
    [active1 setHidden:NO];
    [self.view addSubview:active2];
    [active2 setHidden:YES];
    [self.view addSubview:active3];
    [active3 setHidden:YES];
    [self.view addSubview:active4];
    [active4 setHidden:YES];
	
	// Setup event handlers so that the buttonClicked method will respond to the touch up inside event.
	[btn1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
	[btn2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
	[btn3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
	[btn4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonClicked:(id)sender
{
	int tagNum = ((UIButton*)sender).tag;
	[self selectTab:tagNum];
}

- (void)selectTab:(TabItems)tabtype
{
    if (self.selectedIndex==tabtype) {
        return;
    }
    self.selectedIndex = tabtype;

    [self.active1 setHidden:YES];
    [self.active2 setHidden:YES];
    [self.active3 setHidden:YES];
    [self.active4 setHidden:YES];
    if (self.backgroundView.hidden==NO) {
        if (self.selectedIndex == 0) {
            [self.active1 setHidden:NO];
        }
        else if (self.selectedIndex == 1) {
            [self.active2 setHidden:NO];
        }
        else if (self.selectedIndex == 2) {
            [self.active3 setHidden:NO];
        }
        else if (self.selectedIndex == 3) {
            [self.active4 setHidden:NO];
        }
    }

    	
	
	self.selectedIndex = tabtype;
	DBLOG(@"CMCTabbarController - selectTab:%d",tabtype);
	
}

- (void)dealloc {
	[btn1 release];
	[btn2 release];
	[btn3 release];
	[btn4 release];

    [super dealloc];
}

@end
